package com.example.mybookaidl.provider

import android.os.Parcel
import android.os.Parcelable
import com.example.mybookaidl.aidl.Book
import java.io.Serializable

class User : Parcelable, Serializable {
    @JvmField
    var userId: Int = 0
    @JvmField
    var userName: String? = null
    @JvmField
    var isMale = false
    @JvmField
    var book: Book? = null

    constructor() {}
    constructor(userId: Int, userName: String?, isMale: Boolean) {
        this.userId = userId
        this.userName = userName
        this.isMale = isMale
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(out: Parcel, flags: Int) {
        out.writeInt(userId)
        out.writeString(userName)
        out.writeInt(if (isMale) 1 else 0)
        out.writeParcelable(book, 0)
    }

    private constructor(parcel: Parcel) {
        userId = parcel.readInt()
        userName = parcel.readString()
        isMale = parcel.readInt() == 1
        book = parcel.readParcelable(Thread.currentThread().contextClassLoader)
    }

    override fun toString(): String {
        val line = "User:{userId:%s, userName:%s, isMale:%s}, with child:{%s}"
        return line.format(
                userId, userName, isMale, book)
    }

    companion object {
        private const val serialVersionUID = 519067123721295773L
        val CREATOR: Parcelable.Creator<User?> = object : Parcelable.Creator<User?> {
            override fun createFromParcel(`in`: Parcel): User? {
                return User(`in`)
            }

            override fun newArray(size: Int): Array<User?> {
                return arrayOfNulls(size)
            }
        }
    }
}